package com.shigen.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shigen.demo.entity.LoginLog;
import com.shigen.demo.entity.req.LoginLogReq;
import com.shigen.demo.mapper.LoginLogMapper;
import com.shigen.demo.service.ILoginLogService;
import org.springframework.stereotype.Service;

@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements ILoginLogService {

  /**
   * 登录日志的id和location的分页查询
   *
   * @param queryItem
   * @return
   */
  public IPage<LoginLog> getLoginLogs(LoginLogReq.QueryItem queryItem) {
    Page<LoginLog> page = new Page<>(queryItem.getPagenum(), queryItem.getPagesize());

    String param = queryItem.getParam();
    LambdaQueryChainWrapper<LoginLog> chainWrapper = lambdaQuery();
    if (StrUtil.isNotBlank(param)) {
      chainWrapper.likeRight(LoginLog::getId, param).or().likeRight(LoginLog::getLocation, param);
    }
    return chainWrapper.orderByDesc(LoginLog::getLoginTime).page(page);
  }
}
